package com.erb.controller.llb;

import com.alibaba.fastjson.JSONArray;
import com.erb.entity.*;
import com.erb.service.hzg.lonsauditService;
import com.erb.service.llb.IOtherService;
import com.erb.service.llb.InvestService;
import com.erb.service.zwj.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目控制器
 *
 * @author south
 */
@RestController
@RequestMapping("/base/product")
public class ProductController {
	@Autowired
	IOtherService otherService;
	@Autowired
	InvestService investService;
	@Autowired
	lonsauditService lonsauditService;
	@Autowired
	private IProductService productService;

	/**
	 * 获取项目列表
	 *
	 * @param productExtends
	 * @param pager
	 * @return
	 */
	@GetMapping("/product-list")
	public RespBean productList(ProductExtends productExtends, Pager pager) {
		System.out.println("============" + productExtends);
		pager.page(productService.findCount(productExtends));
		List<Product> productList = productService.findAllForSimpleList(pager, productExtends);
		Map map = new HashMap(2);
		map.put("productList", productList);
		map.put("pager", pager);
		return RespBean.ok("查询成功", map);
	}

	/**
	 * 获取项目详情
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public Map<String, Object> get(@PathVariable("id") Integer id) {
		Map<String, Object> map = new HashMap<>(3);

		Product productById = productService.findProductById(id);
		if (null == productById) {
			map.put("code", 1);
			map.put("msg", "投资项目不存在！");
		} else {
			map.put("code", 0);
			map.put("msg", "查找成功！");
			map.put("object", productById);
		}

		return map;
	}

	@GetMapping("/loan/{lid}")
	public RespBean loanDetail(@PathVariable int lid) {
		return RespBean.ok("查询成功", lonsauditService.seluid(lid));
	}

	/**
	 * 该项目的投资
	 *
	 * @param pid
	 * @param pager
	 * @return
	 */
	@GetMapping("/invest/{pid}")
	public RespBean findInvestByPro(@PathVariable int pid, Pager pager) {
		pager.page(otherService.findTotalInvestByPro(pid));
		List<Invest> invests = otherService.findInvestByPro(pager, pid);
		Map map = new HashMap(2);
		map.put("invests", invests);
		map.put("pager", pager);
		return RespBean.ok("查询成功", map);
	}

	/**
	 * 当前登陆用户的投资次数
	 *
	 * @param session
	 * @return
	 */
	@GetMapping("/countInvest")
	public RespBean countInvest(HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (user != null) {
			return RespBean.ok("查询成功", otherService.countInvest(user.getId()));
		} else {
			return RespBean.ok("当前未登录用户");
		}
	}

	/**
	 * 查询平台标和散表的投资人数
	 *
	 * @param official
	 * @param loan
	 * @return
	 */
	@GetMapping("/sumUsersByProType")
	public RespBean sumUsersByProType(int official, int loan) {
		int sumOfficial = investService.sumUsersByProType(official);
		int sumLoan = investService.sumUsersByProType(loan);
		Map map = new HashMap(2);
		map.put("official", sumOfficial);
		map.put("loan", sumLoan);
		return RespBean.ok("查询成功", map);
	}

	/**
	 * 获取不同投资金额的人数
	 *
	 * @param pid
	 * @return
	 */
	@GetMapping("/countInvestByMoney/{pid}")
	public RespBean countInvestByMoney(@PathVariable int pid) {

		long count1 = 0;
		long count2 = 0;
		long count3 = 0;
		long count4 = 0;
		long count5 = 0;
		long count6 = 0;

		List<Map> mapList = investService.countInvestByMoney(pid);

		for (Map map : mapList) {
			String type = map.get("type").toString();
			if ("1万以下".equals(type)) {
				count1 = (long) (map.get("num"));
			} else if ("1~3万".equals(type)) {
				count2 = (long) (map.get("num"));
			} else if ("3~5万".equals(type)) {
				count3 = (long) (map.get("num"));
			} else if ("5~10万".equals(type)) {
				count4 = (long) (map.get("num"));
			} else if ("10~15万".equals(type)) {
				count5 = (long) (map.get("num"));
			} else if ("15万以上".equals(type)) {
				count6 = (long) (map.get("num"));
			}
		}


		long[] counts = {count1, count2, count3, count4, count5, count6};

		Map data = new HashMap();

		String[] legendData = {"1万以下", "1~3万", "3~5万", "5~10万", "10~15万", "15万以上"};

		data.put("legendData", legendData);

		JSONArray seriesData = new JSONArray();
		for (int i = 0; i < legendData.length; i++) {
			Map map = new HashMap<>();
			map.put("name", legendData[i]);
			map.put("value", counts[i]);
			seriesData.add(map);
		}

		data.put("seriesData", seriesData);

		return RespBean.ok("查询成功", data);
	}

	@GetMapping("/countInvestBySex/{pid}")
	public RespBean countInvestBySex(@PathVariable int pid) {
		List<Map> sexMap = investService.countInvestBySex(pid);

		Map data = new HashMap();

		String[] legendData = {"男", "女"};
		data.put("legendData", legendData);

		long sumMan = 0;
		long sumWoman = 0;

		for (Map map : sexMap) {
			if (map.get("sex") != null) {
				if ("男".equals(map.get("sex").toString())) {
					sumMan = (long) map.get("num");
				}
				if ("女".equals(map.get("sex").toString())) {
					sumWoman = (long) map.get("num");
				}
			}
		}
		JSONArray seriesData = new JSONArray();
		Map man = new HashMap<>();
		man.put("name", "男");
		man.put("value", sumMan);
		seriesData.add(man);

		Map woman = new HashMap<>();
		woman.put("name", "女");
		woman.put("value", sumWoman);
		seriesData.add(woman);

		data.put("seriesData", seriesData);
		return RespBean.ok("查询成功", data);
	}
}
